<?php
global_config::get_instance()->add_server_class("SoapPhantomMessageOfChains","soap_pmessage");
class soap_pmessage
{
	public $Messages = array();

	public function save_to_db()
	{

		$name = '';
		$dba = db_adapter::get_instance();
		$phantommessage = new phantommessage();
		$phantommessage_id = '';
		$to_get_id = new phantommessage();

		$temp = $dba->get_last_id($to_get_id);
			
		$chain_ids = array();
		foreach($this->Messages->as_array() as $soap_message){
			$chain_ids[] = $soap_message->save_to_db();
		}
				
		$chain_ids = array_unique($chain_ids);
		$array_cross = array();
		$i = 0;
		do
		{
			$array_buf = array();
			$chain_phantommessage = new chain_phantommessage();
			$criteria = new query_criteria();
			$criteria->add_criterion($chain_phantommessage->chain_id , criterion_type::$equal, $chain_ids[$i]);
			$dba = db_adapter::get_instance();
			$chain_phantommessage_list = $dba->get_list($chain_phantommessage,$criteria);
			while($item = $chain_phantommessage_list->get_next()) {
				$array_buf[]=$item->phantommessage_id->get_value();
			}
				
			if (!empty($array_cross))
			{
				$array_cross = array_intersect($array_cross, $array_buf);
			}else{
				$array_cross = $array_buf;
			}

			$i++;
		}while(!empty($array_cross) && $i<count($chain_ids));
		

		foreach ($array_cross as $phant_id) {
			$chain_cross = new chain_phantommessage();
			$dba = db_adapter::get_instance();
			$criteria = new query_criteria();
			$criteria->add_criterion($chain_phantommessage->phantommessage_id , criterion_type::$equal, $phant_id);
			$chain_cross_list = $dba->get_list($chain_cross,$criteria);
			if (count($chain_ids)==$chain_cross_list->count())
			{
				return $phant_id;
			}
		}

		if (empty($phantommessage_id))
		{
			$phantommessage = new phantommessage();
			$new_count = $to_get_id->phantommessage_id->get_value() + 1;
			$phantommessage_name = "_".$new_count;
			$phantommessage->phantommessage_name->set_value($phantommessage_name);
			$dba->save_unique($phantommessage);
			$phantommessage_id = $phantommessage->phantommessage_id->get_value();


			foreach($chain_ids as $ch_id){
				$chain_phantommessage = new chain_phantommessage();
				$chain_phantommessage->phantommessage_id->set_value($phantommessage_id);
				$chain_phantommessage->chain_id->set_value($ch_id);
				$dba->save_unique($chain_phantommessage);
			}
		}

		return $phantommessage_id;
	}

	public function load_from_db($phantommessage_id)
	{
		$this->Phantommessage_id = $phantommessage_id;
		$dba = db_adapter::get_instance();
		$chain_phantommessage = new chain_phantommessage();
		$criteria = new query_criteria();
		$criteria->add_criterion($chain_phantommessage->phantommessage_id,criterion_type::$equal,$phantommessage_id);
		$chain_phantommessage_list = $dba->get_list($chain_phantommessage,$criteria);

		$this->Messages = new soap_array_of_chain_value_char();
		while ($chain = $chain_phantommessage_list->get_next(false, true))
		{
			$temp  = new soap_chain();
			$temp->load_from_db($chain->chain_phantommessage->chain_id->get_value());
			$this->Messages->add($temp);
		}
	}
}

?>